package com.awonsome.oss.autoconfigure;

import com.alibaba.cloud.context.AliCloudAuthorizationMode;
import com.alibaba.cloud.spring.boot.context.autoconfigure.AliCloudContextAutoConfiguration;
import com.alibaba.cloud.spring.boot.context.env.AliCloudProperties;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.awonsome.oss.env.OssProperties;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import static com.awonsome.oss.constant.OssConstants.PREFIX;

/**
 * OSS 自动配置
 *
 * @author yangdejun
 * @date 2022/1/5 23:22
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "com.awonsome.oss.autoconfigure.OssAutoConfiguration")
@ConditionalOnProperty(prefix = PREFIX, name = "enabled", matchIfMissing = true)
@EnableConfigurationProperties(OssProperties.class)
@ImportAutoConfiguration(AliCloudContextAutoConfiguration.class)
public class OssContextAutoConfiguration {

    @ConditionalOnMissingBean
    @Bean
    public OSS ossClient(AliCloudProperties aliCloudProperties, OssProperties ossProperties) {
        if (ossProperties.getAuthorizationMode() == AliCloudAuthorizationMode.AK_SK) {
            Assert.isTrue(!StringUtils.isEmpty(ossProperties.getEndpoint()), "Oss endpoint can't be empty");
            Assert.isTrue(!StringUtils.isEmpty(aliCloudProperties.getAccessKey()), "${alibaba.cloud.access-key} can't be empty.");
            Assert.isTrue(!StringUtils.isEmpty(aliCloudProperties.getSecretKey()), "${alibaba.cloud.secret-key} can't be empty.");
            return new OSSClientBuilder().build(ossProperties.getEndpoint(),
                    aliCloudProperties.getAccessKey(),
                    aliCloudProperties.getSecretKey(),
                    ossProperties.getConfig());
        } else if (ossProperties.getAuthorizationMode() == AliCloudAuthorizationMode.STS) {
            Assert.isTrue(!StringUtils.isEmpty(ossProperties.getEndpoint()),
                    "Oss endpoint can't be empty.");
            Assert.isTrue(!StringUtils.isEmpty(ossProperties.getSts().getAccessKey()),
                    "Access key can't be empty.");
            Assert.isTrue(!StringUtils.isEmpty(ossProperties.getSts().getSecretKey()),
                    "Secret key can't be empty.");
            Assert.isTrue(!StringUtils.isEmpty(ossProperties.getSts().getSecurityToken()),
                    "Security Token can't be empty.");
            return new OSSClientBuilder().build(ossProperties.getEndpoint(),
                    ossProperties.getSts().getAccessKey(),
                    ossProperties.getSts().getSecretKey(),
                    ossProperties.getSts().getSecurityToken(), ossProperties.getConfig());
        } else {
            throw new IllegalArgumentException("Unknow auth mode.");
        }
    }
}
